package com.vaadin.tutorial.crm.ui.view.list;

import com.vaadin.tutorial.crm.backend.service.CourseService;
import com.vaadin.tutorial.crm.backend.service.TeacherService;
import com.vaadin.tutorial.crm.ui.MainLayout;
import com.vaadin.flow.component.Tag;
import com.vaadin.flow.component.button.Button;
import com.vaadin.flow.component.button.ButtonVariant;
import com.vaadin.flow.component.grid.Grid;
import com.vaadin.flow.component.grid.GridVariant;
import com.vaadin.flow.component.html.Div;
import com.vaadin.flow.component.orderedlayout.HorizontalLayout;
import com.vaadin.flow.component.orderedlayout.VerticalLayout;
import com.vaadin.flow.component.textfield.TextField;
import com.vaadin.flow.data.value.ValueChangeMode;
import com.vaadin.flow.router.PageTitle;
import com.vaadin.flow.router.Route;
import com.vaadin.tutorial.crm.backend.entity.Course;
import com.vaadin.tutorial.crm.backend.entity.Teacher;
import org.springframework.security.access.annotation.Secured;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestMapping;


@Route(value = "course-list", layout = MainLayout.class)
@PageTitle("courcourse-listseList")
@Tag("courseList-view")
public class CourseView extends VerticalLayout {
    private CourseService courseService;
    private CourseForm courseForm;

    private Grid<Course> grid = new Grid<>(Course.class);
    private TextField filterText = new TextField();

    public CourseView(CourseService courseService, TeacherService teacherService) {
        this.courseService = courseService;
        configureGrid();

        //表单
        courseForm = new CourseForm(teacherService.findAll());
        courseForm.addListener(CourseForm.SaveEvent.class, this::saveCourse);
        courseForm.addListener(CourseForm.DeleteEvent.class, this::deleteCourse);
        courseForm.addListener(CourseForm.CloseEvent.class, e -> closeEditor());

        //将表格、表单添加到一个div
        Div div = new Div(grid, courseForm);
        div.addClassName("main");
        div.setSizeFull();

        //将toolbar、div添加到界面
        add(getToolbar(), div);
        updateList("");
        closeEditor();
    }
    
    private void configureGrid() {
        Button button = new Button("代课老师");
        button.addThemeVariants(ButtonVariant.MATERIAL_CONTAINED,
                ButtonVariant.LUMO_PRIMARY);
        grid.addClassName("course-grid");
        grid.addThemeVariants(GridVariant.LUMO_ROW_STRIPES);
        grid.removeColumnByKey("courseId");
        grid.removeColumnByKey("teacher");
        grid.removeColumnByKey("courseNum");
        grid.removeColumnByKey("courseName");
//        grid.setSizeFull();
        grid.addColumn("courseNum").setHeader("课程编号");
        grid.addColumn("courseName").setHeader("课程名称");
        grid.addColumn(course -> {
            Teacher teacher = course.getTeacher();
            return teacher == null ? "-" : teacher.getTeacherName();
        }).setHeader(button);
        grid.getColumns().forEach(col -> col.setAutoWidth(true));

        //点击表格中的某一列添加到表单中
        grid.asSingleSelect().addValueChangeListener(event ->
                editCourse(event.getValue()));
    }

    //搜索框和添加按钮作为一个toolbar
    private HorizontalLayout getToolbar() {
        filterText.setPlaceholder("请输入课程编号或名称...");
        filterText.setClearButtonVisible(true);
        filterText.setValueChangeMode(ValueChangeMode.LAZY);
        filterText.addValueChangeListener(e -> updateList(e.getValue()));

        Button addCourseButton = new Button("新增课程信息");
        addCourseButton.addClickListener(click -> addCourse());

        HorizontalLayout toolbar = new HorizontalLayout(filterText, addCourseButton);
        toolbar.addClassName("toolbar");
        return toolbar;
    }
    
    //更新表格
    private void updateList(String value) {
        grid.setItems(courseService.findAll(value));
    }

    //清除表单的值
    private void closeEditor() {
        courseForm.setCourse(null);
        courseForm.setVisible(false);
        removeClassName("editing");
    }

    public void editCourse(Course course) {
        if (course == null) {
            closeEditor();
        } else {
            courseForm.setCourse(course);
            courseForm.setVisible(true);
            addClassName("editing");
        }
    }

    //保存
    private void saveCourse(CourseForm.SaveEvent event) {
        courseService.save(event.getCourse());
        updateList("");
        closeEditor();
    }

    //删除
    private void deleteCourse(CourseForm.DeleteEvent event) {
        courseService.delete(event.getCourse());
        updateList("");
        closeEditor();
    }

    void addCourse() {
        grid.asSingleSelect().clear();
        editCourse(new Course());
    }
}
